package com.lottery.im.view;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lottery.im.R;
import java.util.ArrayList;
import java.util.List;

/**
 * 连武凤 2018/10/16 17:22:01
 */
public class BallLinearLayout extends LinearLayout {

  // 数字
  private int[] numBallColors = {
      R.color.pk10_45e083, R.color.pk1_e7c640, R.color.pk2_5ba9f8, R.color.pk3_6a6a6a,
      R.color.pk4_fc9030, R.color.pk5_31e3d8, R.color.pk6_396ef4, R.color.pk7_b5b5b5,
      R.color.pk8_ff5c5c, R.color.pk9_be1e1e, R.color.pk10_45e083, R.color.pk11_c9792b
  };

  // 大小、单双、龙虎
  private int[] dxBallColors = { R.color.pk4_fc9030, R.color.gray_cc };

  private List<RelativeLayout> mViews = new ArrayList<>();
  private List<TextView> mTextViews = new ArrayList<>();

  public BallLinearLayout(Context context) {
    super(context);
    setOrientation(HORIZONTAL);
    initBall();
  }

  public BallLinearLayout(Context context,
      @Nullable
          AttributeSet attrs) {
    super(context, attrs);
    setOrientation(HORIZONTAL);
    initBall();
  }

  public BallLinearLayout(Context context,
      @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setOrientation(HORIZONTAL);
    initBall();
  }

  private void initBall() {
    for (int i = 0; i < 10; i++) {
      RelativeLayout relativeLayout = new RelativeLayout(getContext());
      LinearLayout.LayoutParams params =
          new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
      relativeLayout.setLayoutParams(params);
      TextView textView = new TextView(getContext());
      textView.setGravity(Gravity.CENTER);
      textView.setTextColor(Color.WHITE);
      textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
      int size = (int) getResources().getDimension(R.dimen.space4);
      RelativeLayout.LayoutParams layoutParams =
          new RelativeLayout.LayoutParams(size, size);
      layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
      relativeLayout.addView(textView, layoutParams);
      relativeLayout.setVisibility(GONE);
      addView(relativeLayout);
      mViews.add(relativeLayout);
      mTextViews.add(textView);
    }
  }

  public void setData(int type, List<String> results, List<String> selects, int index) {

    if (results.size() > 10) {
      results = results.subList(0, 10);
    }

    for (RelativeLayout view : mViews) {
      view.setVisibility(GONE);
    }

    switch (index) {
      case 0:
        addNumBall(type, results, selects);
        break;
      case 1:
        addDxBall(type, results);
        break;
      case 2:
        addDsBall(type, results);
        break;
      case 3:
        addLhBall(type, results);
        break;
    }
  }

  private void addNumBall(int type, List<String> results, List<String> selects) {
    for (int i = 0; i < results.size(); i++) {
      String str = results.get(i);
      mViews.get(i).setVisibility(VISIBLE);
      mTextViews.get(i).setTextColor(ContextCompat.getColor(getContext(), R.color.white));
      mTextViews.get(i).setText(str);
      setSize(mTextViews.get(i));
      if (type == 1) {
        mTextViews.get(i).setBackgroundResource(numBallColors[Integer.parseInt(str)]);
      } else if (type == 2) {
        mTextViews.get(i).setBackgroundResource(getResourceId("lottery_ball_0" + str));
      } else if (type == 3) {
        mTextViews.get(i).setBackgroundResource(getResourceId("lottery_ball_" + str));
      }

      if (selects.contains(str)) {
        mTextViews.get(i).getBackground().setAlpha(255);
      } else {
        mTextViews.get(i).getBackground().setAlpha(30);
      }
    }
  }

  private void addDxBall(int type, List<String> results) {
    for (int i = 0; i < results.size(); i++) {
      String str = results.get(i);
      mViews.get(i).setVisibility(VISIBLE);
      mTextViews.get(i).setTextColor(ContextCompat.getColor(getContext(), R.color.white));
      setSize(mTextViews.get(i));
      if (Integer.parseInt(str) > 5) {
        mTextViews.get(i).setText("大");
        if (type == 1) {
          mTextViews.get(i).setBackgroundResource(dxBallColors[0]);
        } else if (type == 2 || type == 3) {
          mTextViews.get(i).setBackgroundResource(R.drawable.lottery_ball_big);
        }
      } else {
        mTextViews.get(i).setText("小");
        if (type == 1) {
          mTextViews.get(i).setBackgroundResource(dxBallColors[1]);
        } else if (type == 2 || type == 3) {
          mTextViews.get(i).setBackgroundResource(R.drawable.lottery_ball_small);
        }
      }
    }
  }

  private void addDsBall(int type, List<String> results) {
    for (int i = 0; i < results.size(); i++) {
      String str = results.get(i);
      mViews.get(i).setVisibility(VISIBLE);
      mTextViews.get(i).setTextColor(ContextCompat.getColor(getContext(), R.color.white));
      setSize(mTextViews.get(i));
      if (Integer.parseInt(str) % 2 == 0) {
        mTextViews.get(i).setText("双");
        if (type == 1) {
          mTextViews.get(i).setBackgroundResource(dxBallColors[0]);
        } else if (type == 2 || type == 3) {
          mTextViews.get(i).setBackgroundResource(R.drawable.lottery_ball_big);
        }
      } else {
        mTextViews.get(i).setText("单");
        if (type == 1) {
          mTextViews.get(i).setBackgroundResource(dxBallColors[1]);
        } else if (type == 2 || type == 3) {
          mTextViews.get(i).setBackgroundResource(R.drawable.lottery_ball_small);
        }
      }
    }
  }

  private void addLhBall(int type, List<String> results) {

    for (int i = 0; i < results.size(); i++) {
      String str = results.get(i);
      mViews.get(i).setVisibility(VISIBLE);

      mTextViews.get(i).setText(str);

      if (type == 1) {
        setSize(mTextViews.get(i));
        if ("龙".equals(str)) {
          mTextViews.get(i).setBackgroundResource(dxBallColors[0]);
        } else {
          mTextViews.get(i).setBackgroundResource(dxBallColors[1]);
        }
      } else if (type == 2 || type == 3) {
        setMatch(mTextViews.get(i));
        mTextViews.get(i).setBackgroundResource(R.color.white);
        switch (i) {
          case 0:
            mTextViews.get(i).setTextColor(ContextCompat.getColor(getContext(), R.color.red_fa));
            break;
          case 1:
            if ("大".equals(str) || "中".equals(str)) {
              mTextViews.get(i).setTextColor(ContextCompat.getColor(getContext(), R.color.red_fa));
            } else {
              mTextViews.get(i).setTextColor(ContextCompat.getColor(getContext(), R.color.gray_66));
            }
            break;
          case 2:
            if ("双".equals(str)) {
              mTextViews.get(i).setTextColor(ContextCompat.getColor(getContext(), R.color.red_fa));
            } else {
              mTextViews.get(i).setTextColor(ContextCompat.getColor(getContext(), R.color.gray_66));
            }
            break;
          case 3:
            if ("龙".equals(str)) {
              mTextViews.get(i).setTextColor(ContextCompat.getColor(getContext(), R.color.red_fa));
            } else if ("虎".equals(str)) {
              mTextViews.get(i).setTextColor(ContextCompat.getColor(getContext(), R.color.gray_66));
            } else {
              mTextViews.get(i).setTextColor(ContextCompat.getColor(getContext(), R.color.blue_4f));
            }
            break;
          default:
            if ("豹子".equals(str) || "对子".equals(str)) {
              mTextViews.get(i).setTextColor(ContextCompat.getColor(getContext(), R.color.red_fa));
            } else {
              mTextViews.get(i).setTextColor(ContextCompat.getColor(getContext(), R.color.gray_66));
            }
            break;
        }
      }
    }
  }

  //根据图片名称取得对应图片
  public int getResourceId(String name) {
    int resId =
        getContext().getResources()
            .getIdentifier(name, "drawable", getContext().getPackageName());
    //解析资源文件夹下，id为resId的图片
    return resId;
  }

  private void setSize(TextView textView) {
    ViewGroup.LayoutParams lp = textView.getLayoutParams();
    int size = (int) getResources().getDimension(R.dimen.space4);
    lp.width = size;
    lp.height = size;
    textView.setLayoutParams(lp);
  }

  private void setMatch(TextView textView) {
    ViewGroup.LayoutParams lp = textView.getLayoutParams();
    lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
    lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
    textView.setLayoutParams(lp);
  }
}
